package com.air.admin.controller.imp;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.admin.controller.IRoleManagerController;
import com.air.admin.mo.AdminRole;
import com.air.admin.service.IAdminRoleService;
import com.air.admin.service.IBasicAdminInfoService;
import com.air.admin.util.AdminAppConstant;
import com.air.admin.util.AdminErrorCode;
import com.air.admin.vo.RoleQueryRequestVO;
import com.air.common.controller.imp.BaseControllerImp;
import com.air.common.util.BasicAppConstant;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.common.vo.PageResultListVO;
import com.air.common.vo.ResponseVO;

@Controller
@RequestMapping("/admin/roleManager")
public class RoleManagerControllerImp extends BaseControllerImp implements
		IRoleManagerController {
	
	@Autowired
	IAdminRoleService  adminRoleService;
	
	@Autowired
	IBasicAdminInfoService adminInfoService;
	
	@Override
	@RequestMapping("addRole")
	public ModelAndView addRole(HttpServletRequest request,
			HttpServletResponse response, AdminRole role)  throws Exception{
		
		role = adminRoleService.insert(role);
		ResponseVO responseVO = new ResponseVO();
		responseVO.addObject("role", role);
		return parse2Mav(request,responseVO);
	}

	@Override
	@RequestMapping("listRole")
	public ModelAndView listRole(HttpServletRequest request,
			HttpServletResponse response, RoleQueryRequestVO queryRequestVO)
			throws Exception {
		
		QueryCondition condition = new QueryCondition();
		
		if (queryRequestVO.getSort()!=null){
			condition.addOrderExpression(queryRequestVO.getSort(), queryRequestVO.getOrder());
		}
		PageResultListVO pageResult;
		
		if (queryRequestVO.getQueryMode()==null||queryRequestVO.getQueryMode().equalsIgnoreCase(AdminAppConstant._QUERY_MODE_SIMPLE)){
			pageResult = adminRoleService.queryInSimpleMode(condition, queryRequestVO.getPage()-1, queryRequestVO.getRows(), queryRequestVO.getSimpleQueryString());
		}else{
			pageResult = adminRoleService.queryInAdvanceMode(condition,  queryRequestVO.getPage()-1, queryRequestVO.getRows(), queryRequestVO);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.addObject(BasicAppConstant._KEY_RESPONSE_VO,JSONObject.fromObject(pageResult).toString());
		mav.setViewName(BasicAppConstant._VIEW_COMMON_JSON);
		return mav;
	}

	@Override
	@RequestMapping("removeRoles")
	public ModelAndView removeRoles(HttpServletRequest request,
			HttpServletResponse response, String roleList) {
		
		List<String> roleCodeList = Arrays.asList(roleList.split(","));
		
		ResponseVO responseVO = new ResponseVO();
		
		if (adminInfoService.hasRelativeAdmin(roleCodeList)){
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(AdminErrorCode._ERRCODE_HAS_RELATIVE_ADMIN);
			return parse2Mav(request, responseVO);
		}
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("code", roleCodeList, QueryExpression.IN);
		adminRoleService.deleteByCondition(condition);
		
		return parse2Mav(request, responseVO);
	}

	@Override
	@RequestMapping("updateRole")
	public ModelAndView updateRole(HttpServletRequest request,
			HttpServletResponse response, AdminRole role) {
		
		this.adminRoleService.updateById(role);
		ResponseVO responseVO = new ResponseVO();
		return parse2Mav(request, responseVO);
		
	}

}
